package com.ldatum.crm.action.admin;

import java.io.File;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	
	private long  maxSize = 100000;
	private String allowedTypes = ".jpg, .jpeg, .bmp, .gif";
	
	public void doUpload() throws Exception{
		PrintWriter out = null;
		JSONObject json = new JSONObject();
//		BufferedReader br = null;
//		BufferedWriter bw = null;
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			
			String path = ServletActionContext.getServletContext().getRealPath("upload");
			System.out.println(path);
			File uploadFile = new File(path);
			//如果文件不存在，则建立文件。
			if(!uploadFile.exists()){
				uploadFile.mkdir();
			}
			
			//验证文件大小
			if(maxSize < uploadFileName.length()){
				json.accumulate("alertMessage", "超出文件限定大小");
				json.accumulate("result", false);
			}
			
			//验证文件类型
			boolean flag = true;
			String[] allowedTypesStr = allowedTypes.split(",");
			for(String type:allowedTypesStr){
				if (uploadContentType.equals(type)) {  
	                flag = true;  
	            }
			}
			
			if(flag == false){
				json.accumulate("alertMessage", "文件类型不合法!");
				json.accumulate("result", false);
			}
			
			//1，以字节为单位传输文件;
//			FileInputStream input = new FileInputStream(upload);
//			FileOutputStream output = new FileOutputStream(uploadFile + "//" + uploadFileName);
//			try {
//				byte[] b = new byte[1024];
//				int i = 0;
//				while((i = input.read(b)) > 0){
//					output.write(b,0,i);
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//			}finally{
//				input.close();
//				output.close();
//				upload.delete();
//			}
			
			//2，Struts2封装的一种方式；
			FileUtils.copyFile(upload, new File(uploadFile+"\\"+uploadFileName));
			upload.delete();
			
			//3，以字符的方式传输文件。
//			br = new BufferedReader(new InputStreamReader(new FileInputStream(upload)));
//			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadFile+"\\"+uploadFileName)));
//			char[] c = new char[1024];
//			int i = 0;
//			while((i = br.read(c)) > 0 ){
//				bw.write(c,0,i);
//			}
			json.accumulate("alertMessage", "上传成功！");
			json.accumulate("result", true);
			
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("alertMessage", "上传失败");
			json.accumulate("result", false);
		}finally{
//			br.close();
//			bw.close();
//			upload.delete();
			out.println(json.toString());
			out.flush();
			out.close();
		}
		
		
	}
	
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public long getMaxSize() {
		return maxSize;
	}
	public void setMaxSize(long maxSize) {
		this.maxSize = maxSize;
	}
	public String getAllowedTypes() {
		return allowedTypes;
	}
	public void setAllowedTypes(String allowedTypes) {
		this.allowedTypes = allowedTypes;
	}
	
	
	
}
